package ahg;

import static org.junit.Assert.assertTrue;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Tests that CakeBot moves away from walls after running into them.
 * @author Tony Gaskell
 *
 */
public class TestCakeBotWall extends RobotTestBed {
  /** True if the robot turns around upon hitting a wall.
   */
  boolean turnAround = false;
  
  /**
   * Retains the heading from the previous turn.
   */
  double previousHeading = 0;
  
  /**
   * Specifies that SittingDuck and CakeBot are to be matched up in this test case.
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.SittingDuck,ahg.CakeBot";
  }
  
  /**
   * This test runs for 1 round.
   * @return The number of rounds. 
   */
  @Override
  public int getNumRounds() {
    return 1;
  }
  
  /**
   * Checks if the the robot changes its heading after hitting a wall.
   * @param event Info about the current state of the battle.
   */
  @Override 
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double xPos = robot.getX();
    double yPos = robot.getY();
    //Checks west wall
    if ((xPos < 40) && (robot.getBodyHeading() != previousHeading)) {
      turnAround = true;
    }
    //Checks east wall
    if ((xPos > (width - 40)) && (robot.getBodyHeading() != previousHeading)) {
      turnAround = true;
    }
    //Checks south wall
    if ((yPos < 40) && (robot.getBodyHeading() != previousHeading)) {
      turnAround = true;
    }
    //Checks north wall
    if ((yPos > (height - 40)) && (robot.getBodyHeading() != previousHeading)) {
      turnAround = true;
    }
    previousHeading = robot.getBodyHeading();
  }
  
  
  /**
   * After the battle, check to see that we've visited the corners. 
   * @param event Details about the completed battle.
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    assertTrue("Turned around after hitting a wall", turnAround);
    
  }

}
